package com.sita.tingterest.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.rest.model.Category;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

/**
 * Created by nilesh on 11/7/2015.
 */
public class MusicGridViewAdapter extends BaseAdapter {


    private Context context;
    private List<Category> MusicList = null;

    public MusicGridViewAdapter(Context context, List<Category> musicList) {
        this.context = context;
        this.MusicList = musicList;

    }


    @Override
    public int getCount() {

        Log.i("MY TAG", "MusicList.size();" + MusicList.size());

        return MusicList.size();
    }

    @Override
    public Object getItem(int position) {
        return MusicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public static class ViewHolder {
        public ImageView img;
        public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder view = null;
        if (convertView == null) {
            view = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.ting_grid_row, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.title_song);
            view.img = (ImageView) convertView.findViewById(R.id.pic);

            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();

        }
        // Log.i("MY TAG","MusicList.get(position).getAuthorId()"+MusicList.get(position).getAuthorId());
        view.txtViewTitle.setText(MusicList.get(position).mName);
        //Log.i("MY TAG", "MusicList.get(position). getImage()" + MusicList.get(position).getImage());

        // new GetImage(view,convertView ).execute(MusicList.get(position).getPic().getUrl());
        //view.img.setImageResource(R.mipmap.ic_launcher);
        view.img.setImageBitmap(stringtoBitmap(MusicList.get(position).mCover.mData));

        // todo get image from server
        return convertView;
    }

    private class GetImage extends AsyncTask<String, String, Bitmap> {
        private byte[] iconData;
        private ViewHolder view;
        private View converView;
        private Bitmap bmp;


        public GetImage(ViewHolder view, View convertView)

        {
            Log.i("MY TAG", "GetImage in AsynTask");
            this.view = view;
            this.converView = convertView;
        }

        @Override
        protected Bitmap doInBackground(String... params) {


            try {
                InputStream in = new URL(params[0]).openStream();
                Log.i("MY TAG", "params[0]" + params[0]);
                bmp = BitmapFactory.decodeStream(in);
            } catch (Exception e) {

            }
            return bmp;

        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {

            view.img.setImageBitmap(bitmap);

            super.onPostExecute(bitmap);
        }
    }

    public Bitmap stringtoBitmap(String string) {

// 将字符串转换成Bitmap类型

        Bitmap bitmap = null;

        try {

            byte[] bitmapArray;

            bitmapArray = Base64.decode(string, Base64.DEFAULT);

            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,

                    bitmapArray.length);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return bitmap;

    }
}
